package cn.daniellee.plugin.ril.listener;

import cn.daniellee.plugin.ril.ItemLoreLimiter;
import cn.daniellee.plugin.ril.model.LimitedLore;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityPickupItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;

public class PlayerListener implements Listener {

	@EventHandler
	public void onPlayerPickupItem(EntityPickupItemEvent e) {
		if (e.getEntity() instanceof Player) {
			LimitedLore limitedLore = ItemLoreLimiter.getInstance().getLimitLoreByItemStack(e.getItem().getItemStack());
			if (limitedLore != null) {
				if (!e.getEntity().hasPermission(limitedLore.getPermission())) {
					e.setCancelled(true);
				}
			}
		}
	}

	@EventHandler
	public void onPlayerInteract(PlayerInteractEvent e) {
		for (ItemStack itemStack : e.getPlayer().getInventory().getContents()) {
			LimitedLore limitedLore = ItemLoreLimiter.getInstance().getLimitLoreByItemStack(itemStack);
			if (limitedLore != null) {
				if (!e.getPlayer().hasPermission(limitedLore.getPermission())) {
					e.getPlayer().getLocation().getWorld().dropItem(e.getPlayer().getLocation(), itemStack).setPickupDelay(20);
				}
			}
		}
	}

}
